package com.yf.monitor.config;

import com.yf.monitor.ApiMonitorProperties;
import com.yf.monitor.aspect.ApiMonitorAspect;
import com.yf.monitor.service.ApiRecordService;
import com.yf.monitor.service.TDengineService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * @ClassName ApiMonitorAutoConfiguration
 * @Description 注入
 * @Author Scott.Yin
 * @Date 2025/8/21 17:25
 * @Version 1.0
 **/
@Configuration
@ConditionalOnClass(ApiMonitorAspect.class)
@EnableConfigurationProperties(ApiMonitorProperties.class)
@ConditionalOnProperty(prefix = "api.monitor", value = "enabled", havingValue = "true", matchIfMissing = true)
public class ApiMonitorAutoConfiguration {

//    @Bean
//    @ConditionalOnMissingBean
//    public TDengineService tdengineService(DataSource dataSource, ApiMonitorProperties properties) {
//        return new TDengineService(dataSource, properties);
//    }

    @Bean
    @ConditionalOnMissingBean
    public TDengineService tdengineService(ApiMonitorProperties properties) {
        return new TDengineService(properties);
    }

    @Bean
    @ConditionalOnMissingBean
    public ApiRecordService apiRecordService(ApiMonitorProperties properties) {
        return new ApiRecordService( null, properties); // 需要RocketMQTemplate和TDengineService
    }

    @Bean
    @ConditionalOnMissingBean
    public ApiMonitorAspect apiMonitorAspect(ApiRecordService apiRecordService) {
        return new ApiMonitorAspect(apiRecordService);
    }

//    @Bean
//    @ConditionalOnMissingBean
//    public AnomalyDetectionService anomalyDetectionService(
//            JdbcTemplate jdbcTemplate, ApiMonitorProperties properties) {
//        return new AnomalyDetectionService(jdbcTemplate, properties);
//    }
}
